package com.yourcompany.web.controller;

import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yourcompany.api.dto.UserLoginDTO;
import com.yourcompany.api.dto.UserQueryDTO;
import com.yourcompany.api.entity.User;
import com.yourcompany.api.service.UserService;
import com.yourcompany.api.config.UserProperties;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户管理")
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserProperties userProperties;

    @Operation(summary = "注册")
    @PostMapping("/register")
    public SaResult register(@Validated @RequestBody UserLoginDTO registerDTO) {
        userService.register(registerDTO);
        return SaResult.ok();
    }

    @Operation(summary = "修改密码")
    @PostMapping("/updatePassword")
    public SaResult updatePassword(@RequestParam String oldPassword,
                                 @RequestParam String newPassword) {
        userService.updatePassword(userService.getCurrentUser().getId(), 
                oldPassword, newPassword);
        return SaResult.ok();
    }

    @Operation(summary = "重置密码")
    @PostMapping("/resetPassword")
    public SaResult resetPassword(@RequestParam String phone) {
        userService.resetPassword(phone);
        return SaResult.ok();
    }

    @Operation(summary = "获取当前用户信息")
    @GetMapping("/current")
    public SaResult getCurrentUser() {
        return SaResult.ok().setData(userService.getCurrentUser());
    }

    @Operation(summary = "分页查询用户列表")
    @GetMapping("/list")
    public SaResult pageUsers(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "查询条件") UserQueryDTO queryDTO) {
        Page<User> page = new Page<>(pageNum, pageSize);
        return SaResult.ok().setData(userService.pageUsers(page, queryDTO));
    }

    /**
     * 获取默认密码
     *
     * 该接口根据传入的手机号返回默认密码。如果提供了手机号，则返回手机号的后6位作为默认密码；
     * 如果未提供手机号，则返回配置文件中配置的默认密码。
     *
     * @param phone 手机号，可选参数。如果提供，则使用手机号的后6位作为默认密码。
     * @return 返回一个包含默认密码的SaResult对象。如果操作成功，SaResult的data字段将包含默认密码。
     */
    @Operation(summary = "获取默认密码")
    @GetMapping("/open/defaultPassword")
    public SaResult getDefaultPassword(@Parameter(description = "手机号") @RequestParam(required = false) String phone) {
        String defaultPassword;
        if (StringUtils.hasText(phone)) {
            // 如果提供了手机号，返回手机号后6位
            defaultPassword = phone.substring(Math.max(0, phone.length() - 6));
        } else {
            // 否则返回配置的默认密码
            defaultPassword = userProperties.getDefaultPassword();
        }
        return SaResult.ok().setData(defaultPassword);
    }
} 